/**
 * SearchImageImpl.java Create on 2012-7-30     
*      
* Copyright (c) 2012-7-30 by yiming     
*      
* @author <a href="alenzhangym@hotmail.com">Yiming</a>     
* @version 1.0 
 */
package com.yimi.search;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.yimi.actions.SearchListener;
import com.yimi.service.BaseImageItem;
import com.yimi.service.Entity;
import com.yimi.service.ISearchService;
import com.yimi.service.ISearchServiceCallBack;
import com.yimi.service.ImageList;

import java.util.Vector;
 
/**
 * TODO ��������    
 *      
 *  
 *      
 * @author Yiming
 * @version 1.0 
 */
public class SearchImageServiceImpl implements SearchImageService {
    private static SearchImageService searchImage;
    private Application app;
    private ISearchService tService;
    private ISearchServiceCallBack callback;
    private Vector<SearchListener> searchListeners;
    private Handler handler = new Handler();
    private ServiceConnection connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
           
            Log.e("SearchServiceImpl", "onServiceConnected");
            tService = ISearchService.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName name) {
            Log.i("ServiceConnection  onServiceDisconnected", "dis connect");
            tService = null;
        }
    };
    
    public void startService() {
        Log.e(this.getClass().getName(), "startService");
        searchListeners = new Vector<SearchListener>();
        Intent service = new Intent(ISearchService.class.getName());
        this.app.bindService(service, connection,
                Context.BIND_AUTO_CREATE);
        initCallBack();
        while(tService==null){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            tService.registerListeners(callback);
        } catch (RemoteException e) { 
            e.printStackTrace();
        }
        
    }
    
    /**
     * 
     */
    private void initCallBack() { 
        callback = new ISearchServiceCallBack.Stub() {
            @Override
            public void onEvent(final int event,final  int type, final Entity result)  {
             Log.e(this.getClass().getName(), "Onevent is "+event+result.toString());
             handler.post(new Runnable(){
                    @Override
                    public void run() {
                        for(int i=0;i<searchListeners.size();i++){
                            SearchListener searchListner = searchListeners.elementAt(i);
                            searchListner.onEvent(event, type, result);
                        } 
                        Log.e(this.getClass().getName(),"callback over");
                    }
                    
                });
            }
            
        };
    }

    private SearchImageServiceImpl(Application app){
        this.app = app;
    }
    
    public static synchronized SearchImageService getInstance(Application app){
        if(searchImage==null){
            searchImage = new SearchImageServiceImpl(app);
        }
        return searchImage;
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImage#queryImages(int)
     */
    @Override
    public void queryImages(int page,String queryName) throws RemoteException {
         this.tService.queryImagesEntity(String.valueOf(page), queryName);
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImage#registerSearchListener(com.yimi.actions.SearchListener)
     */
    @Override
    public  void registerSearchListener(SearchListener listener) {
        // TODO Auto-generated method stub
        synchronized(searchListeners){
            searchListeners.add(listener);
        }
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImage#unRegisterSearchListener(com.yimi.actions.SearchListener)
     */
    @Override
    public void unRegisterSearchListener(SearchListener listner) {
        synchronized(searchListeners){
            searchListeners.remove(listner);
        }
    }

    /* 
     * 查询列表中的预览图
     * 
     * @see com.yimi.search.SearchImageService#queryPreview(com.yimi.service.ImageList)
     */
    @Override
    public void queryPreview(int page,ImageList list) {
        // TODO Auto-generated method stub
       try {
        this.tService.queryImagePreviews(page, list);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImageService#queryBigImage(java.lang.String)
     */
    @Override
    public void queryImageFileView(String fileURL) {
        try {
            this.tService.queryImageFileView(fileURL);
        } catch (RemoteException e) { 
            e.printStackTrace();
        }
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImageService#cancelDownloadImage(java.lang.String)
     */
    @Override
    public void cancelDownloadImage(String fileURL) {
        try {
            this.tService.cancelDownloadImage(fileURL);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /* (non-Javadoc)
     * @see com.yimi.search.SearchImageService#queryPreview(com.yimi.service.BaseImageItem)
     */
    @Override
    public void queryPreview(BaseImageItem baseItem) {
        
        try {
            this.tService.queryPreview(baseItem);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
